<template>
	<view>
		<view class='bargain-list'>
			<!-- #ifdef H5 -->
			<view class='iconfont icon-xiangzuo' @tap='goBack' :style="'top:'+ (navH/2) +'rpx'" v-if="returnShow">
			</view>
			<!-- #endif -->

      <!-- 砍价记录的概要 -->
			<view class='header'>
				<view class="pic">
					<view class='swipers'>
						<swiper :indicator-dots="indicatorDots" :autoplay="autoplay" interval="2500" duration="500" vertical="true"
						 circular="true">
							<block v-for="(item,index) in bargainSuccessList" :key='index'>
								<swiper-item >
									<view class="acea-row row-middle">
										<image :src="item.avatar" class="mr9"></image>
										<view class='mr9 nickName'>{{ item.nickname }}</view>
										<text class='mr9'>拿了</text>
										<view class='line1'>{{ item.activityName }}</view>
									</view>
								</swiper-item>
							</block>
						</swiper>
					</view>
				</view>
				<view class="tit">已有{{ bargainTotal }}人砍成功</view>
			</view>

      <!-- 砍价活动列表 -->
			<view class='list'>
				<block v-for="(item,index) in bargainList" :key="index">
					<view class='item acea-row row-between-wrapper'
                @tap="openSubscribe('/pages/activity/goods_bargain_details/index?id='+ item.id +'&startBargainUid='+ uid)">
						<view class='pictrue'>
							<image :src='item.picUrl'></image>
						</view>
						<view class='text acea-row row-column-around'>
							<view class='name line2'>{{ item.name }}</view>
							<view class="acea-row" style="margin-bottom: 14rpx;">
								<countDown :tipText="' '" :bgColor="bgColor" :dayText="':'" :hourText="':'" :minuteText="':'" :secondText="' '"
                           :datatime="item.endTime / 1000" :isDay="true" />
								<text class="txt">后结束</text>
							</view>
							<view v-if="item.stock === 0">
								<view style="font-size: 22rpx;" @tap="openSubscribe('/pages/activity/goods_bargain_details/index?id='+ item.id +'&startBargainUid='+ uid)">已售罄</view>
							</view>
							<view class='money font-color'>最低: ￥<text class='price'>{{ fen2yuan(item.bargainMinPrice) }}</text></view>
						</view>
						<view v-if="item.stock > 0" class='cutBnt bg-color'>参与砍价</view>
						<view v-if="item.stock === 0" class='cutBnt bg-color-hui'>已售罄</view>
					</view>
				</block>
				<view class='loadingicon acea-row row-center-wrapper' v-if='bargainList.length > 0'>
					<text class='loading iconfont icon-jiazai' :hidden='!loading'></text>{{loadTitle}}
				</view>
			</view>
		</view>
		<home></home>
	</view>
</template>
<script>
  let app = getApp();
	import { openBargainSubscribe } from '@/utils/SubscribeMessage.js';
	import home from '@/components/home';
	import countDown from '@/components/countDown';
	import { mapGetters } from "vuex";
  import * as BargainApi from '@/api/promotion/bargain.js';
  import * as Util from '@/utils/util.js';
  export default {
		components: {
			countDown,
			home,
		},
		data() {
			return {
        navH: '',
        returnShow: true,

        // ========== 砍价记录概要的相关变量 ==========
        bargainTotal: 0,
        bargainSuccessList: [],
        autoplay: true,
        indicatorDots: false,

        // ========== 砍价活动的相关变量 ==========
        bargainList: [],
        page: 1,
        limit: 10,
        loading: false,
        loadend: false,
        bgColor: {
					'bgColor': '#E93323',
					'Color': '#fff',
					'width': '44rpx',
					'timeTxtwidth': '16rpx',
					'isDay': true
				},
				loadTitle: '加载更多',
			};
		},
		computed: mapGetters(['isLogin', 'uid']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getBargainList();
						this.getBargainHeader();
					}
				},
				deep: true
			}
		},
		onLoad: function() {
			const pages = getCurrentPages();
			this.returnShow = pages.length !== 1;
			uni.setNavigationBarTitle({
				title: "砍价列表"
			})
			this.navH = app.globalData.navHeight;

      // 获得砍价信息
      this.getBargainList();
      this.getBargainHeader();
		},
		methods: {
      /**
       * 获得砍价记录的概要
       */
			getBargainHeader: function() {
        BargainApi.getBargainRecordSummary().then(res => {
          this.bargainTotal = res.data.successUserCount;
          this.bargainSuccessList = res.data.successList;
        }).catch(err => {
          return this.$util.Tips({
            title: err
          });
        })
			},
      /**
       * 获得砍价活动列表
       */
      getBargainList: function() {
        if (this.loadend || this.loading) {
          return;
        }
        this.loading = true;
        this.loadTitle = '';
        BargainApi.getBargainActivityPage({
          pageNo: this.page,
          pageSize: this.limit
        }).then(res => {
          const list = res.data.list;
          const bargainList = this.$util.SplitArray(list, this.bargainList);
          const loadend = list.length < this.limit;
          this.loadend = loadend;
          this.loading = false;
          this.loadTitle = loadend ? '已全部加载' : '加载更多';
          this.$set(this, 'bargainList', bargainList);
          this.$set(this, 'page', this.page + 1);
        }).catch(res => {
          this.loading = false;
          this.loadTitle = '加载更多';
        });
      },
      /**
       * 跳转到砍价详情
       *
       * @param e 跳转的地址
       */
			openSubscribe: function(e) {
				let page = e;
				// #ifndef MP
				uni.navigateTo({
					url: page
				});
				// #endif
				// #ifdef MP
				uni.showLoading({
					title: '正在加载',
				})
				openBargainSubscribe().then(res => {
					uni.hideLoading();
					uni.navigateTo({
						url: page,
					});
				}).catch((err) => {
					uni.hideLoading();
				});
				// #endif
			},
      goBack: function() {
        uni.navigateBack({
          delta: 1
        });
      },

      fen2yuan(price) {
        return Util.fen2yuan(price)
      }
		},
		onReachBottom: function() {
			this.getBargainList();
		}
	}
</script>
<style lang="scss">
	page {
		background-color: #E93323 !important;
	}
</style>
<style lang="scss" scoped>
     .mr9{
		 margin-right: 9rpx;
	 }
	.swipers {
		height: 100%;
		width: 76%;
		margin: auto;
		overflow: hidden;
		font-size: 22rpx;
		color: #fff;
        image{
			width: 24rpx;
			height: 24rpx;
			border-radius: 50%;
			overflow: hidden;
		}
		swiper {
			height: 100%;
			width: 100%;
			overflow: hidden;
		}
		.line1{
			width: 195rpx;
		}
	}

	.bargain-list .icon-xiangzuo {
		font-size: 40rpx;
		color: #fff;
		position: fixed;
		left: 30rpx;
		z-index: 99;
		transform: translateY(-20%);
	}

	.bargain-list .header {
		background-image: url('');
		babackground-repeat: no-repeat;
		background-size: 100% 100%;
		width: 750rpx;
		height: 420rpx;
        .acea-row{
        	height: 50rpx;
			line-height: 50rpx;
			left: 50rpx;
			.nickName{
				width: 65rpx;
				overflow: hidden;
				white-space: nowrap;
			}
        }
		.pic {
			width: 478rpx;
			height: 50rpx;
			margin: 0 auto;
			background-image: url('../static/lun.png');
			babackground-repeat: no-repeat;
			background-size: 100% 100%;
		}

		.tit {
			color: #FFFFFF;
			font-size: 24rpx;
			font-weight: 400;
			text-align: center;
			margin-top: 304rpx;
		}
	}

	.bargain-list .list {
		padding: 0 30rpx;
	}

	.bargain-list .list .item {
		position: relative;
		height: 250rpx;
		background-color: #fff;
		border-radius: 14rpx;
		margin-bottom: 20rpx;
		padding: 30rpx 25rpx;
	}

	.bargain-list .list .item .pictrue {
		width: 190rpx;
		height: 190rpx;
	}

	.bargain-list .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 14rpx;
	}

	.bargain-list .list .item .text {
		width: 432rpx;
		font-size: 28rpx;
		color: #333333;

		.txt{
			font-size: 22rpx;
			margin-left: 4rpx;
			color: #666666;
			line-height: 36rpx;
		}
	}

	.bargain-list .list .item .text .name {
		width: 100%;
		height: 68rpx;
		line-height: 36rpx;
		font-size: 28rpx;
		margin-bottom: 26rpx;
	}

	.bargain-list .list .item .text .num {
		font-size: 26rpx;
		color: #999;
	}

	.bargain-list .list .item .text .num .iconfont {
		font-size: 35rpx;
		margin-right: 7rpx;
	}

	.bargain-list .list .item .text .money {
		font-size: 24rpx;
		font-weight: bold;
	}

	.bargain-list .list .item .text .money .price {
		font-size: 38rpx;
	}

	.bargain-list .list .item .cutBnt {
		position: absolute;
		width: 162rpx;
		height: 52rpx;
		border-radius: 50rpx;
		font-size: 24rpx;
		color: #fff;
		text-align: center;
		line-height: 52rpx;
		right: 24rpx;
		bottom: 30rpx;
		background: linear-gradient(90deg, #FF7931 0%, #E93323 100%);
	}

	.bargain-list .list .item .cutBnt .iconfont {
		margin-right: 8rpx;
		font-size: 30rpx;
	}

	.bargain-list .list .load {
		font-size: 24rpx;
		height: 85rpx;
		text-align: center;
		line-height: 85rpx;
	}
</style>
